<%@ page language="java" contentType="application/x-javascript; charset=UTF-8" pageEncoding="UTF-8" %>
<%@ include file="/commons/pages/taglibs.jsp" %>
<c:if test="${false}">
<script type="text/javascript">
//</c:if>
var ctx = '${ctx}';
var code_data = function (){
	var CAPITALS = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
	var data= [];
	for (var i = 0; i < CAPITALS.length; i++) {
		var o = {};
		o.text = CAPITALS[i];
		o.value = CAPITALS[i];
		data.push(o);
	}
	return data;
}();

//序列化 JSON.stringify(Object o)
//对象化 $('#fm').serializeObject()
$.fn.serializeObject = function() {  
    var o = {};  
    var a = this.serializeArray();  
    $.each(a, function() {  
        if (o[this.name]) {  
            if (!o[this.name].push) {  
                o[this.name] = [ o[this.name] ];  
            }  
            o[this.name].push(this.value || '');  
        } else {  
            o[this.name] = this.value || '';  
        }  
    });  
    return o;  
};

function initCombobox(ctx) {
	$("select[id='is_del']").each(function(i){ //删除
		$(this).combobox({
			panelHeight: 'auto',
			editable: false,
			url: ctx + '/platform/type-info/json?type_sign=BOOLEAN',
		    valueField: 'type_value',  
		    textField: 'type_name',
		    value: 0
		});
	});
	
	$("select[id='is_lock'],select[id='is_safety'],select[id='is_public']").each(function(i){//删除,是否安规产品,是否公共
		$(this).combobox({
			panelHeight: 'auto',
			editable: false,
			url: ctx + '/platform/type-info/json?type_sign=BOOLEAN',
		    valueField: 'type_value',  
		    textField: 'type_name'
		    //value: i % 2 == 0 ? '' : 0
		});
	});
	
	$("select[id='gender']").each(function(i){//性别
		$(this).combobox({
			panelHeight: 'auto',
			editable: false,
			url: ctx + '/platform/type-info/json?type_sign=GENDER',
		    valueField: 'type_value',
		    textField: 'type_name'
		});
	});
	
	$("select[id='role_codes']").each(function(){//角色
		$(this).combobox({
			panelHeight: 'auto',
			editable: false,
			url: ctx + '/platform/role-info/json',
		    valueField: 'role_code',  
		    textField: 'role_name',
		    multiple:true
		});
	});
	
	$("select[id='part_level']").each(function(){//器件等级
		$(this).combobox({
			panelHeight: 'auto',
			editable: false,
			url: ctx + '/platform/type-info/json?type_sign=PART_LEVEL',
		    valueField: 'type_code',  
		    textField: 'type_name'  
		});
	});

	$("select[id='sel_level']").each(function(){//优选等级
		$(this).combobox({
			panelHeight: 'auto',
			editable: false,
		    url: ctx + '/platform/type-sel-level/json',  
		    valueField: 'type_code',  
		    textField: 'type_name'  
		});
	});
	
	$("select[id='type_pcode']").each(function(){//器件代码
		$(this).combobox({
			panelHeight: 'auto',
			editable: false,
		    url: ctx + '/platform/type-part-code/json',  
		    valueField: 'type_code',  
		    textField: 'type_name'  
		});
	});
}

$(function(){
	initCombobox(ctx);
	if ($('#dg')) {//datagrid随窗口变化大小
		$(window).resize(function() {
			$('#dg').datagrid('resize');
		});
	}
});

var MSG_INFO    = '提示';
var MSG_CONFIRM = '确认';
var MSG_WARNING = '警告';
var MSG_ERROR   = '错误';

function doSearch(cfg){
	var cfg   = cfg       || {};
	var grid  = cfg.grid  || 'datagrid';
	var url   = cfg.url   || (grid == 'treegrid' ? $('#dg').treegrid('options').url : $('#dg').datagrid('options').url);
	var title = cfg.title || '搜索';
	
	if (grid == 'treegrid'){
		$.post(url, $('#fm_search').serializeObject(), function(data){
		 	$('#dg').treegrid('loadData', data);
		}, 'json');
	} else {
		$('#dg').datagrid('load', $('#fm_search').serializeObject()); 
	}
}

function doSave(cfg){
	var cfg   = cfg       || {};
	var grid  = cfg.grid  || 'datagrid';
	var url   = cfg.url   || (grid == 'treegrid' ? $('#dg').treegrid('options').url : $('#dg').datagrid('options').url);
	var title = cfg.title || '保存';
	var oper  = cfg.oper  || 'confirm';
	
	url = url.replace('search', 'save');

	if ('multipart/form-data' == $('#fm').attr('enctype')){
        var options = { 
            url: url,
            beforeSubmit: function (){
            	return $('#fm').form('validate');
            },
		    uploadProgress: function(event, position, total, percentComplete) {
				$('#p').show().progressbar('setValue', percentComplete);
				if (100 == percentComplete) {
					$('#p').hide();
				}
		    },
			success: function(result) {
				var result = eval('(' + result + ')');
				if (result.success) {
					if ($('#id').val()) {
						$.messager.show({title:MSG_INFO, timeout:3000, msg:'保存数据成功!'});
						$('#dlg').dialog('close');
						doSearch({url: url.replace('save', 'search'), grid:grid});
					} else {
						$.messager.confirm(MSG_CONFIRM, '保存成功，是否继续添加数据？', function(r){
							if (!r){
								$('#dlg').dialog('close');
								doSearch({url: url.replace('save', 'search'), grid:grid});
							}
						});
					}
				} else if (!result.success) {
					$.messager.show({title:MSG_ERROR, timeout:3000, msg:result.msg});
				}
			}
        };
        $('#fm').ajaxSubmit(options);
        return false; 
	} 
	
	$('#fm').form('submit', {
		url: url,
	    onSubmit: function(){  
	        return $(this).form('validate');
	    },  
	    success: function(result){
	    	var result = eval('(' + result + ')');
			if (result.success){
				switch (oper) {
					case 'confirm':
						if ($('#id').val()) {
							$.messager.show({title:MSG_INFO, timeout:3000, msg:'保存数据成功!'});
							$('#dlg').dialog('close');
							doSearch({url: url.replace('save', 'search'), grid:grid});
						} else {
							$.messager.confirm(MSG_CONFIRM, '保存成功，是否继续添加数据？', function(r){
								if (!r){
									$('#dlg').dialog('close');
									doSearch({url: url.replace('save', 'search'), grid:grid});
								}
							});
						}
						break;
					case 'tip':
						$.messager.show({title:MSG_INFO, timeout:3000, msg:result.msg});
						break;
					case 'login-redirect':
						parent.location.href = ctx + '/platform/main';
						break;
				}
			} else {
			    $.messager.show({title:MSG_INFO, timeout:3000, msg:result.msg});
			}
	    }
	});
}

function doAdd(cfg){
	var cfg   = cfg       || {};
	var grid  = cfg.grid  || 'datagrid';
	var url   = cfg.url   || (grid == 'treegrid' ? $('#dg').treegrid('options').url : $('#dg').datagrid('options').url);
	var title = cfg.title || '添加';
	
	$('#dlg').dialog('open').dialog('setTitle', title);
	$('#fm').form('clear');
	//$('#fm').find('#id').val("");
}

function doEdit(cfg) {
	var cfg   = cfg       || {};
	var grid  = cfg.grid  || 'datagrid';
	var url   = cfg.url   || (grid == 'treegrid' ? $('#dg').treegrid('options').url : $('#dg').datagrid('options').url);
	var title = cfg.title || '修改';
	
	url = url.replace('search', 'edit/');

	var rows = (grid == 'treegrid' ? $('#dg').treegrid('getSelections') : $('#dg').datagrid('getSelections'));
	if (rows.length != 1) {
		$.messager.alert(MSG_WARNING, '请选择一条记录修改', 'warning');
		return;
	}
	$('#dlg').dialog('open').dialog('setTitle', title);
	$('#fm').form('clear');
	$('#fm').form({
    	onLoadSuccess:function(data){
    		if (data && data.role_codes) {//用户之角色代码
    			$('#role_codes').combobox('clear').combobox('setValues', data.role_codes.split(','));
    		}
		}
	}).form('load', url + rows[0].id);
	//$('#fm').form('load', url + rows[0].id);
}

function doRemove(cfg){
	var cfg   = cfg       || {};
	var grid  = cfg.grid  || 'datagrid';
	var url   = cfg.url   || (grid == 'treegrid' ? $('#dg').treegrid('options').url : $('#dg').datagrid('options').url);
	var title = cfg.title || '删除';
	
	url = url.replace('search', 'delete');
	
	var rows = (grid == 'treegrid' ? $('#dg').treegrid('getSelections') : $('#dg').datagrid('getSelections'));
	if (rows.length < 1) {
		$.messager.alert(MSG_WARNING, '请选择至少一条记录！', 'warning');
		return;
	}
	$.messager.confirm(MSG_CONFIRM, '确认删除选中的所有的记录吗？', function(r){  
		if (!r){
			return;
		}
		var ids = [];  
		for(var i = 0; i < rows.length; i++){  
			ids.push(rows[i].id);  
		}
		$.getJSON(url, { ids: ids.join(','), rnd: Math.random() }, function (result){
			if (result.success){
				$.messager.show({title:MSG_INFO, timeout:3000, msg:'成功删除' + result.count + '条记录!'});
			} else {
				$.messager.show({title:MSG_INFO, timeout:3000, msg:'删除失败!<br />' + result.msg});
			}
			doSearch({url: url.replace('delete', 'search'), grid:grid});
		});
	});
}

//<c:if test="${false}">
</script>
</c:if>